//
//  MainViewController.m
//  LineMoney
//
//  Created by 范琦 on 16/7/8.
//  Copyright © 2016年 范琦. All rights reserved.
//

#import "MainViewController.h"
#import "MainTopView.h"
#import "MainSectionView.h"
#import "MainCell.h"
#import <Foundation/Foundation.h>
#import "NSPoint.h"
#import "ExchangeViewController.h"
#import "ExchangeMainView.h"
#import "ExchangeMainView2.h"

@interface MainViewController ()<UITableViewDataSource, UITableViewDelegate, UIScrollViewDelegate, UIPickerViewDataSource, UIPickerViewDelegate>

@property (nonatomic, strong) MainTopView *topView;
@property (nonatomic, strong) MainSectionView *sectionView1;

@property (nonatomic, strong) MainSectionView *sectionView2;

@property (nonatomic, strong) UIScrollView *scroll;

@property (nonatomic, strong) UIScrollView *mainScroll;

@property (nonatomic, strong) UIView *mainTopView;

@property (nonatomic, strong) UIView *bomttomSectionView;

@property (nonatomic, strong) NSMutableArray *labelArr;

@property (nonatomic, strong) UIPickerView *pickView;
@property (nonatomic, strong) UIView *pickTopView;

@property (nonatomic, strong) NSMutableArray *listArr;

@property (nonatomic, strong) NSString *midStr;
@property (nonatomic, strong) NSString *midNumStr;

@property (nonatomic, assign) BOOL isChange;




@end

@implementation MainViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = NO;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"LineMoney";
    self.view.backgroundColor = HUIBACKGROUNDCOLOR;
    
    self.isChange = YES;
    [self creatMainScroll];
    self.mainTopView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KSCREENWIDTH, 340)];
    [_mainScroll addSubview:_mainTopView];
    _mainTopView.backgroundColor = [UIColor clearColor];
    [self creatSectionView];
    [self creatRightButton];
    [self creatTopView];
    [self creatBottomSectionView];
    [self creatScroll];
    
    [self creatTableView];
    
    
}
#warning 创建所有视图
- (void)creatRightButton
{
    
    UIButton *shareButton = [UIButton buttonWithType:UIButtonTypeCustom];
    shareButton.frame = CGRectMake(0, 0, 50, 30);
    [shareButton setTitle:@"English" forState:UIControlStateNormal];
    shareButton.titleLabel.font = [UIFont systemFontOfSize:12];
    
    [[shareButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        NSLog(@"点击");
    }];
    UIBarButtonItem *barButtonItem1 = [[UIBarButtonItem alloc]initWithCustomView:shareButton];
    self.navigationItem.rightBarButtonItems = @[barButtonItem1];
    
}

- (void)creatTopView
{
    self.topView = [[[NSBundle mainBundle] loadNibNamed:@"MainTopView" owner:nil options:nil] lastObject];
    [_mainTopView addSubview:_topView];
    _topView.frame = CGRectMake(0, 36, KSCREENWIDTH, 195);
    
    [[_topView.toNext rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        self.tabBarController.selectedIndex = 1;
        
        for (UINavigationController *controller in self.tabBarController.viewControllers) {
            
            
            if ([[controller.viewControllers firstObject] isKindOfClass:[ExchangeViewController class]]) {
                ExchangeViewController *exChangeVC = (ExchangeViewController *)[controller.viewControllers firstObject];
                exChangeVC.exchangeStyleStr = _topView.CNY.text;
                exChangeVC.exchangeToStr = _topView.USD.text;
                exChangeVC.exchangeMoneyStr = _topView.CNYText.text;
                exChangeVC.exchangeHLStr = _topView.USDText.text;
            }
        }
        
    }];
    
    [[_topView.exchangeBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
       

        
        [UIView transitionWithView:_topView.USDImg duration:1 options:UIViewAnimationOptionTransitionCurlUp animations:^{
//            _topView.USDImg.frame = CGRectMake(0, 0, 150, 150);
        } completion:^(BOOL finished) {
            NSLog(@"过渡结束");
        }];
        
        [UIView transitionWithView:_topView.CNYImg duration:1 options:UIViewAnimationOptionTransitionCurlUp animations:^{
//            _topView.CNYImg.frame = CGRectMake(0, 0, 150, 150);
        } completion:^(BOOL finished) {
            NSLog(@"过渡结束");
        }];
        
        [UIView transitionWithView:_topView.USD duration:1 options:UIViewAnimationOptionTransitionCurlUp animations:^{
            //            _topView.CNYImg.frame = CGRectMake(0, 0, 150, 150);
        } completion:^(BOOL finished) {
            NSLog(@"过渡结束");
        }];
        
        [UIView transitionWithView:_topView.CNY duration:1 options:UIViewAnimationOptionTransitionCurlUp animations:^{
            //            _topView.CNYImg.frame = CGRectMake(0, 0, 150, 150);
        } completion:^(BOOL finished) {
            NSLog(@"过渡结束");
        }];
        
        
        [UIView transitionWithView:_topView.USDText duration:1 options:UIViewAnimationOptionTransitionCurlUp animations:^{
            //            _topView.CNYImg.frame = CGRectMake(0, 0, 150, 150);
        } completion:^(BOOL finished) {
            NSLog(@"过渡结束");
        }];
        
        [UIView transitionWithView:_topView.CNYText duration:1 options:UIViewAnimationOptionTransitionCurlUp animations:^{
            //            _topView.CNYImg.frame = CGRectMake(0, 0, 150, 150);
        } completion:^(BOOL finished) {
            NSLog(@"过渡结束");
        }];
        
        self.midStr = _topView.CNY.text;
        _topView.CNY.text = _topView.USD.text;
        _topView.USD.text = _midStr;
        if (_isChange == YES) {
            _topView.USDImg.image = [UIImage imageNamed:@"china"];
            _topView.CNYImg.image = [UIImage imageNamed:@"USD"];

            _isChange = NO;
        }else
        {
            _topView.USDImg.image = [UIImage imageNamed:@"USD"];
            _topView.CNYImg.image = [UIImage imageNamed:@"china"];

            _isChange = YES;
        }
        
        self.midNumStr = _topView.CNYText.text;
        _topView.CNYText.text = _topView.USDText.text;
        _topView.USDText.text = _midNumStr;
        
    }];
    
    [[_topView.CNYBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        [self creatPickerView:x];
        
    }];
    [[_topView.USDBty rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        [self creatPickerView:x];
        
    }];

}
- (void)creatSectionView
{
    self.sectionView1 = [[[NSBundle mainBundle] loadNibNamed:@"MainSectionView" owner:nil options:nil] lastObject];
    [_mainTopView addSubview:_sectionView1];
    _sectionView1.frame = CGRectMake(0, 0, KSCREENWIDTH, 36);
    _sectionView1.imageV.hidden = YES;
    
    self.sectionView2 = [[[NSBundle mainBundle] loadNibNamed:@"MainSectionView" owner:nil options:nil] lastObject];
    [_mainTopView addSubview:_sectionView2];
    _sectionView2.frame = CGRectMake(0, 231, KSCREENWIDTH, 46);
    _sectionView2.label.text = @"服务价格表";
    _sectionView2.imageV.hidden = YES;
}
- (void)creatScroll
{

    
    
    self.scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, _sectionView2.frame.origin.y + 46 + 33, KSCREENWIDTH, 10 * 44)];
    [_mainScroll addSubview:_scroll];
    _scroll.contentSize = CGSizeMake(KSCREENWIDTH * 3, 0);
    _scroll.pagingEnabled = YES;
    
    [RACObserve(_scroll, contentOffset) subscribeNext:^(id x) {
        NSValue *toValue = (NSValue *)x;
        CGPoint c;
        [toValue getValue:&c];
        NSInteger i = c.x / KSCREENWIDTH;
        UILabel *label1 = [self.view viewWithTag:i + 2000];
        for (UILabel *label in _labelArr) {
            if (label == label1) {
                label.textColor = BACKGROUNDCOLOR;

            }else{
                label.textColor = [UIColor blackColor];
            }
        }
    }];
}
- (void)creatMainScroll
{
    self.mainScroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, KSCREENWIDTH, KSCREENHEIGHT)];
    [self.view addSubview:_mainScroll];
    _mainScroll.backgroundColor = HUIBACKGROUNDCOLOR;
    _mainScroll.contentSize = CGSizeMake(0, 380 + 10 * 44);
    _mainScroll.showsVerticalScrollIndicator = NO;
}

// 循环创建三个button和label 把三个label放在一个数组内 当点击时 根据button的tag值 +1000 找到对应的label 循环label数组, 发现label相同则改为点击状态 否则改为未点击状态
- (void)creatBottomSectionView
{
    NSArray *arr = @[@"≥50000.0", @"≥30000.0", @"≥10000.0"];
    self.labelArr = [NSMutableArray array];
    self.bomttomSectionView = [[UIView alloc]initWithFrame:CGRectMake(0, _sectionView2.frame.origin.y + 46, KSCREENWIDTH, 33)];
    _bomttomSectionView.backgroundColor = [UIColor whiteColor];
    [_mainScroll addSubview:_bomttomSectionView];
    for (int i = 0; i < 3; i++) {
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(i * (KSCREENWIDTH / 3), 0, KSCREENWIDTH / 3, 33)];
        label.text = arr[i];
        label.textAlignment = NSTextAlignmentCenter;
        label.font = [UIFont systemFontOfSize:12];
        label.tag = i + 2000;
        [_labelArr addObject:label];
        if (i == 0) {
            label.textColor = BACKGROUNDCOLOR;
        }
        
        [_bomttomSectionView addSubview:label];
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        button.frame = label.frame;
        [_bomttomSectionView addSubview:button];
        button.tag = i + 1000;
        [[button rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            UIButton *b = x;
            [_scroll setContentOffset:CGPointMake((b.tag - 1000) * KSCREENWIDTH, 0) animated:YES];
            UILabel *label1 = (UILabel *)[self.view viewWithTag:b.tag + 1000];
            for (UILabel *label in _labelArr) {
                if (label == label1) {
                    label.textColor = BACKGROUNDCOLOR;
                }else{
                    label.textColor = [UIColor blackColor];
                }
            }
        }];
    }
    


}
- (void)creatTableView
{
    for (int i = 0; i < 3; i++) {
        UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(i * KSCREENWIDTH, 0, KSCREENWIDTH, 10 * 44) style:UITableViewStylePlain];
        [_scroll addSubview:tableView];
        tableView.delegate = self;
        tableView.dataSource = self;

        tableView.scrollEnabled = NO;
        tableView.sectionHeaderHeight = 40;
        [tableView registerNib:[UINib nibWithNibName:@"MainCell" bundle:nil] forCellReuseIdentifier:@"cell"];
        tableView.contentInset = UIEdgeInsetsMake(1, 0, 0, 0);
        tableView.backgroundColor = HUIBACKGROUNDCOLOR;
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
    }
}

#warning tableView所有代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 10;
}

// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MainCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

#warning 创建PickerView
- (void)creatPickerView:(UIButton *)btn
{
    if (_pickView == nil) {
        self.listArr = [NSMutableArray arrayWithObjects:@"CNY",@"USD",@"法国", nil];
        self.pickView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, KSCREENHEIGHT - 150, KSCREENWIDTH, 150)];
        _pickView.frame = CGRectMake(0, KSCREENHEIGHT - 144, KSCREENWIDTH, 144);
        _pickView.delegate = self;
        _pickView.dataSource = self;
        [self.view addSubview:_pickView];
        _pickView.backgroundColor = [UIColor huiseColor];
        self.tabBarController.tabBar.hidden = YES;
        
        self.pickTopView = [[UIView alloc]initWithFrame:CGRectMake(0, _pickView.frame.origin.y - 30, KSCREENWIDTH, 30)];
        [self.view addSubview:_pickTopView];
        _pickTopView.backgroundColor = HUIBACKGROUNDCOLOR;
        
    }else{
        self.tabBarController.tabBar.hidden = YES;
        [self.view addSubview:_pickView];
        [self.view addSubview:_pickTopView];
    }
    UIButton *done = [UIButton buttonWithType:UIButtonTypeSystem];
    [done setTitle:@"Done" forState:UIControlStateNormal];
    done.frame = CGRectMake(KSCREENWIDTH - 60, 0, 40, 30);
    done.tintColor = [UIColor blackColor];
    [_pickTopView addSubview:done];
    [[done rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        [_pickView removeFromSuperview];
        [_pickTopView removeFromSuperview];
        if (btn == _topView.CNYBtn) {
            
            _topView.CNY.text = _listArr[[_pickView selectedRowInComponent:0]];
        }else{
            _topView.USD.text = _listArr[[_pickView selectedRowInComponent:0]];

        }
        self.tabBarController.tabBar.hidden = NO;
    }];
    
}
#warning PickerView代理方法
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    
    return 3;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return _listArr[row];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
